﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Xml;

namespace WebRaoVat.Admin
{
    public partial class QuanLyChuyenMuc : System.Web.UI.Page
    {
        public List<DanhMucChinhDTO> danhMucChinh = new List<DanhMucChinhDTO>();
        public List<List<DanhMucConDTO>> tatCaDanhMucCon = new List<List<DanhMucConDTO>>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LaAdmin"] == null || Session["LaAdmin"].ToString().Length <= 0)
            {                
                //Response.Redirect("../Default.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string ten = txtTenDanhMucChinh.Text.Trim();
            if(""==ten)
                return;
            DanhMucChinhDTO _danhmuc = new DanhMucChinhDTO(ten, -1);
            _danhmuc.Deleted = 1;
            DanhMucChinhBUS.Insert(_danhmuc);
            //Server.Transfer("quanlychuyenmuc.aspx");
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string ten = txtDanhMucCon.Text.Trim();
            if ("" == ten)
                return;
            DanhMucConDTO _danhMuc = new DanhMucConDTO(ten, 1, -1, 1);
            DanhMucConBUS.Insert(_danhMuc);
            //Server.Transfer("quanlychuyenmuc.aspx");
            GridView2.DataBind();
        }

        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int madanhmuccon = int.Parse(e.Keys[0].ToString());
            string tendanhmuccon = e.NewValues[0].ToString();

            string tendanhmucchinh = e.NewValues[1].ToString();
            int madanhmucchinh = DanhMucChinhBUS.getMa(tendanhmucchinh);

            int thututrongdanhmucchinh = int.Parse(e.NewValues[2].ToString());
            int del = int.Parse(e.NewValues[3].ToString());

            DanhMucConDTO danhmuc = new DanhMucConDTO(madanhmuccon, tendanhmuccon,
                madanhmucchinh, thututrongdanhmucchinh, del);

            DanhMucConBUS.Update(danhmuc);
            //Server.Transfer("quanlychuyenmuc.aspx");
            GridView2.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //Lấy danh sách danh mục chính từ CSDL
            danhMucChinh = DanhMucChinhBUS.getAll();

            //Lấy danh sách danh mục con từ CSDL (lấy theo danh mục chính)
            List<DanhMucConDTO> dsDanhMucCon;
            for (int iDanhMucChinh = 0; iDanhMucChinh < danhMucChinh.Count; iDanhMucChinh++)
            {
                dsDanhMucCon = new List<DanhMucConDTO>();
                dsDanhMucCon = DanhMucConBUS.layDSDanhMucCon(danhMucChinh[iDanhMucChinh].MaDanhMucChinh);
                tatCaDanhMucCon.Add(dsDanhMucCon);
            }

            XmlDocument taiLieu = null;
            try
            {
                taiLieu = XL_XML.ReadDocumentFile(Server.MapPath("../Gui.xml"));
            }
            catch { }
            if (taiLieu == null)
                return;

            XL_XML.DeleteNode(taiLieu, "//GIAODIEN/MENU");// xóa MENU cũ ra
            ThemMenu(taiLieu);

            //Thêm Danh mục chính vào XML
            for (int i = 0; i < danhMucChinh.Count; i++)
            {
                ThemDanhMucChinh(taiLieu, danhMucChinh[i]);

                //Thêm danh mục con vào XML
                for(int j=0; j<tatCaDanhMucCon[i].Count; j++)
                {
                    ThemDanhMucCon(taiLieu, tatCaDanhMucCon[i][j]);
                }
            }

            XL_XML.WriteDocument(taiLieu, Server.MapPath("../Gui.xml"));
            /*for (int i = 0; i < tatCaDanhMucCon.Count; i++)
            {
                //ThemDanhMucCon(taiLieu, tatCaDanhMucCon[
            }*/
        }

        public void ThemMenu(XmlDocument taiLieu)
        {
            List<string> tenThuocTinh = new List<string>();
            List<string> giaTriThuocTinh = new List<string>();
            tenThuocTinh.Add("NgayDoi"); giaTriThuocTinh.Add(DateTime.Now.ToString());
            XmlNode Menu = XL_XML.CreateNode(taiLieu, "MENU", tenThuocTinh, giaTriThuocTinh);
            XL_XML.AddNode(taiLieu, "//GIAODIEN", Menu);
        }

        public void ThemDanhMucChinh(XmlDocument tailieu, DanhMucChinhDTO danhmucchinh)
        {
            List<string> tenThuocTinh = new List<string>();
            List<string> giaTriThuocTinh = new List<string>();
            tenThuocTinh.Add("ten"); giaTriThuocTinh.Add(danhmucchinh.TenDanhMucChinh);
            tenThuocTinh.Add("ma"); giaTriThuocTinh.Add(danhmucchinh.MaDanhMucChinh.ToString());

            XmlNode NodeDanhMuc = XL_XML.CreateNode(tailieu, "DANHMUCCHINH", tenThuocTinh, giaTriThuocTinh);

            XL_XML.AddNode(tailieu, "//GIAODIEN/MENU", NodeDanhMuc);
        }

        public void ThemDanhMucCon(XmlDocument tailieu, DanhMucConDTO danhMucCon)
        {
            List<string> tenThuocTinh = new List<string>();
            List<string> giaTriThuocTinh = new List<string>();
            tenThuocTinh.Add("ten"); giaTriThuocTinh.Add(danhMucCon.TenDanhMucCon);
            tenThuocTinh.Add("ma"); giaTriThuocTinh.Add(danhMucCon.MaDanhMucCon.ToString());

            XmlNode NodeDanhMuc = XL_XML.CreateNode(tailieu, "DANHMUCCON", tenThuocTinh, giaTriThuocTinh);

            XL_XML.AddNode(tailieu, "//GIAODIEN/MENU/DANHMUCCHINH[@ma='" + danhMucCon.MaDanhMucChinh + "']", NodeDanhMuc);
        }
    }
}
